package cn.itcast.helloworld;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private MyAdapter myAdapter = null;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnDelete;
    private Button mBtnUpdate;
    private MyHelper myHelper;
    private SQLiteDatabase db;
    public ArrayList<info> list;
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts);
        list = new ArrayList<>();
        myHelper=new MyHelper(this);
        db = myHelper.getReadableDatabase();
        init();
        Cursor cursor = db.rawQuery("select * from information",null);
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            info ci = new info(name,phone);
            list.add(ci);
        }
        myAdapter = new MyAdapter(this,R.layout.list_item,list);
        lv.setAdapter(myAdapter);

        //监听ListView控件的子项
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                info ci = list.get(position);
                Toast.makeText(MainActivity.this, ci.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void init(){
        mBtnAdd = (Button) findViewById(R.id.btn_add);
        mBtnQuery = (Button) findViewById(R.id.btn_query);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        mBtnUpdate = (Button) findViewById(R.id.btn_update);
        lv = (ListView)findViewById(R.id.lv);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
    }


    // 调用onCreate(), 目的是刷新数据,  从另一activity界面返回到该activity界面时, 此方法自动调用
    @Override
    protected void onResume() {
        super.onResume();
        list.clear();
        if(myAdapter!= null){
            Cursor cursor = db.rawQuery("select * from information",null);

            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                info ci = new info(name,phone);
                list.add(ci);
            }
            myAdapter = new MyAdapter(this,R.layout.list_item,list);
            lv.setAdapter(myAdapter);
        }
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.btn_add:
                Intent intent=new Intent(MainActivity.this,twoActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_query:
                break;
        }
    }
}

